export default class extends think.Service {
    constructor() {
        super();
        this.cms_area_model = this.db['cms_area'];
        this.device_catalog_video_model = this.db['device_catalog_video'];
    }
    /**
     * 查询行政区域
     */
    async getAreaData(id) {
        const orgId = `${id}`;
        try {
            return await new this.TreeService().findGrandchildrensAndSelf('cms_area', orgId);
        } catch (error) {
            return this.exceptionService.handleError(error);
        }
    }
    /**
     * 分页获取设备列表
     */
    async getDevice(data) {
        const Op = this.db.Sequelize.Op;
        data.start = (parseInt(data.page) - 1) * parseInt(data.pageSize);
        const where = {};
        if (data.name) {
            where.name = {
                [Op.like]: '%' + data.name + '%'
            };
        }
        try {
            return await this.device_catalog_video_model.findAndCountAll({
                where: where,
                limit: parseInt(data.pageSize),
                offset: parseInt(data.start)
            });
        } catch (error) {
            return this.exceptionService.handleError(error);
        }
    }
    /**
     * 获取设备列表
     */
    async getAllDevice() {
        try {
            return await this.device_catalog_video_model.findAll();
        } catch (error) {
            return this.exceptionService.handleError(error);
        }
    }
    /**
     * 更改设备状态
     */
    async changeDeviceEnable(data) {
        try {
            const where = {
                device_id: data.id
            };
            const parm = {
                is_enable: data.is_enable
            };
            // if (data.is_enable == 1) {
            //     parm.is_enable = 0;
            // } else if (data.is_enable == 0) {
            //     parm.is_enable = 1;
            // }
            return await this.device_catalog_video_model.update(parm, {
                where
            });
        } catch (error) {
            return this.exceptionService.handleError(error);
        }
    }

    /**
     * 更改设备状态
     */
    async changeDeviceInfo(data) {
        try {
            const where = {
                device_id: data.device_id
            };
            // if (data.is_enable == 1) {
            //     parm.is_enable = 0;
            // } else if (data.is_enable == 0) {
            //     parm.is_enable = 1;
            // }
            return await this.device_catalog_video_model.update(data, {
                where
            });
        } catch (error) {
            return this.exceptionService.handleError(error);
        }
    }

    async deleteDevice(data) {
        try {
            const where = {
                device_id: data.device_id
            };
            // if (data.is_enable == 1) {
            //     parm.is_enable = 0;
            // } else if (data.is_enable == 0) {
            //     parm.is_enable = 1;
            // }
            return await this.device_catalog_video_model.destory({
                where
            });
        } catch (error) {
            return this.exceptionService.handleError(error);
        }
    }
}
